//
//  MainViewController.m
//  口袋
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "MainViewController.h"
#import "NewsScrollView.h"
#import "ButtonScrollView.h"
#import "NewsCell.h"
#import "ConnectModel.h"
#import "TodayNewsViewController.h"
#import "TouchScrollView.h"
#import "QHCcomment.h"
#import "SANetWorkingTask.h"
#import "ItemView.h"
#import "ShowImageViewController.h"
#import "NewsModel.h"
#import <MJRefresh/MJRefresh.h>
#import "NewsCollectCell.h"
#import "TodaySubjectViewController.h"
#import "NewsDetailViewController.h"
#import "DKNightVersion.h"


#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface MainViewController ()<UITableViewDataSource, UITableViewDelegate, NewsColledtDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) TouchScrollView * navScroll;

@property (nonatomic, retain) UIView * topView;

@property (nonatomic, retain) UIScrollView * buttonScroll;

@property (nonatomic, retain) UIView * buttonView;

@property (nonatomic, retain) UIScrollView * scrollView;

@property (nonatomic, retain) UIButton * moreButton;

@property (nonatomic, retain) NSMutableArray * buttonArray;

@property (nonatomic, retain) NSMutableArray * buttonArr;

@property (nonatomic, retain) NSMutableArray * buttonsArray;

@property (nonatomic, assign) float startPointX;

@property (nonatomic, assign) NSInteger light;

@end

@implementation MainViewController

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RECEIVE" object:nil];
    [_buttonsArray release];
    [_buttonArr release];
    [_buttonArray release];
    [_collect release];
    [_navScroll release];
    [_topView release];
    [_buttonView release];
    [_moreButton release];
    [_scrollView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.buttonsArray = [NSMutableArray array];
       // self.tabBarController.tabBar.hidden = NO;
        self.light = 1;
        [self createDefaults];
    }
    return self;
}

// 本地存储栏目等
- (void)createDefaults
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * arraa = [defaults objectForKey:@"buttonArray"];
    NSArray * arrrr = [defaults objectForKey:@"buttonArr"];
    if (!arraa) {
        NSDictionary * dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"头条", @"title", @"http://c.3g.163.com/nc/article/headline/T1348647853363/0-140.html", @"url", @"T1348647853363", @"name", nil];
        
        NSDictionary * dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"体育", @"title", @"http://c.3g.163.com/nc/article/list/T1348649079062/0-20.html", @"url", @"T1348649079062", @"name", nil];
        
        NSDictionary * dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"财经", @"title", @"http://c.3g.163.com/nc/article/list/T1348648756099/0-20.html", @"url", @"T1348648756099", @"name", nil];
        
        NSDictionary * dic4 = [NSDictionary dictionaryWithObjectsAndKeys:@"科技", @"title", @"http://c.3g.163.com/nc/article/list/T1348649580692/0-20.html", @"url", @"T1348649580692", @"name", nil];
        
        NSDictionary * dic5 = [NSDictionary dictionaryWithObjectsAndKeys:@"NBA", @"title", @"http://c.3g.163.com/nc/article/list/T1348649145984/0-20.html", @"url", @"T1348649145984", @"name", nil];
        
        NSDictionary * dic6 = [NSDictionary dictionaryWithObjectsAndKeys:@"军事", @"title", @"http://c.3g.163.com/nc/article/list/T1348648141035/0-20.html", @"url", @"T1348648141035", @"name", nil];
        
        NSDictionary * dic7 = [NSDictionary dictionaryWithObjectsAndKeys:@"汽车", @"title", @"http://c.3g.163.com/nc/article/list/T1348654060988/0-20.html", @"url", @"T1348654060988", @"name", nil];
        
        NSDictionary * dic8 = [NSDictionary dictionaryWithObjectsAndKeys:@"轻松一刻", @"title", @"http://c.3g.163.com/nc/article/list/T1350383429665/0-20.html", @"url", @"T1350383429665", @"name", nil];
        
        NSDictionary * dic9 = [NSDictionary dictionaryWithObjectsAndKeys:@"房产", @"title", @"http://c.3g.163.com/nc/article/house/5YWo5Zu9/0-20.html", @"url", @"全国", @"name", nil];
        
        NSDictionary * dic10 = [NSDictionary dictionaryWithObjectsAndKeys:@"时尚", @"title", @"http://c.3g.163.com/nc/article/list/T1348650593803/0-20.html", @"url", @"T1348650593803", @"name", nil];
        
        NSDictionary * dic11 = [NSDictionary dictionaryWithObjectsAndKeys:@"娱乐", @"title", @"http://c.3g.163.com/nc/article/list/T1348648517839/0-20.html",  @"url", @"T1348648517839", @"name", nil];
        
        NSArray * array = @[dic1, dic2, dic3, dic4, dic5, dic6, dic7, dic8, dic9, dic10, dic11];
        self.buttonArray = [NSMutableArray arrayWithArray:array];
        
        [defaults setObject:_buttonArray forKey:@"buttonArray"];
        
        
        NSDictionary * dic111 = [NSDictionary dictionaryWithObjectsAndKeys:@"跑步", @"title", @"http://c.3g.163.com/nc/article/list/T1411113472760/0-20.html", @"url", @"T1411113472760", @"name", nil];
        
        NSDictionary * dic12 = [NSDictionary dictionaryWithObjectsAndKeys:@"社会", @"title", @"http://c.3g.163.com/nc/article/list/T1348648037603/0-20.html", @"url", @"T1348648037603", @"name", nil];
        
        NSDictionary * dic13 = [NSDictionary dictionaryWithObjectsAndKeys:@"历史", @"title", @"http://c.3g.163.com/nc/article/list/T1368497029546/0-20.html", @"url", @"T1368497029546", @"name", nil];
        
        NSDictionary * dic14 = [NSDictionary dictionaryWithObjectsAndKeys:@"彩票", @"title", @"http://c.3g.163.com/nc/article/list/T1356600029035/0-20.html", @"url", @"T1356600029035", @"name", nil];
        
        NSDictionary * dic15 = [NSDictionary dictionaryWithObjectsAndKeys:@"健康", @"title", @"http://c.3g.163.com/nc/article/list/T1414389941036/0-20.html", @"url", @"T1414389941036", @"name", nil];
        
        NSDictionary * dic16 = [NSDictionary dictionaryWithObjectsAndKeys:@"旅游", @"title", @"http://c.3g.163.com/nc/article/list/T1348654204705/0-20.html", @"url", @"T1348654204705", @"name", nil];
        
        NSDictionary * dic17 = [NSDictionary dictionaryWithObjectsAndKeys:@"游戏", @"title", @"http://c.3g.163.com/nc/article/list/T1348654151579/0-20.html", @"url", @"T1348654151579", @"name", nil];
        
        NSDictionary * dic18 = [NSDictionary dictionaryWithObjectsAndKeys:@"教育", @"title", @"http://c.3g.163.com/nc/article/list/T1348654225495/0-20.html", @"url", @"T1348654225495", @"name", nil];
        
        NSDictionary * dic19 = [NSDictionary dictionaryWithObjectsAndKeys:@"数码", @"title", @"http://c.3g.163.com/nc/article/list/T1348649776727/0-20.html", @"url", @"T1348649776727", @"name", nil];
        
        
        NSArray * arr = @[dic111, dic12, dic13, dic14, dic15, dic16, dic17, dic18, dic19];
        self.buttonArr = [NSMutableArray arrayWithArray:arr];
        [defaults setObject:_buttonArr forKey:@"buttonArr"];
        [defaults synchronize];
    } else {
        self.buttonArray = [NSMutableArray arrayWithArray:arraa];
        self.buttonArr = [NSMutableArray arrayWithArray:arrrr];
    }
}


- (void)loadView
{
    [super loadView];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receive:) name:@"RECEIVE" object:nil];
    [self createView];

}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:118/255.0 green:210/255.0 blue:245/255.0 alpha:1.0]];
    self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x343434);
}

#pragma mark -创建界面
- (void)createView
{
    self.buttonsArray = [NSMutableArray array];
    
//    self.navigationItem.title = @"口袋新闻";
//    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    // 左按钮
    UIImage * zouni = [UIImage imageNamed:@"zouni.png"];
    // 图片原始颜色
    zouni = [zouni imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * allDay = [[UIBarButtonItem alloc] initWithImage:zouni style:UIBarButtonItemStylePlain target:self action:@selector(allDayAction:)];
    [self.navigationItem setLeftBarButtonItem:allDay];
    [allDay release];
    
    // 右按钮夜间模式
    if (self.light == 1) {
       // UIBarButtonItem * night = [[UIBarButtonItem alloc] initWithTitle:@"night" style:UIBarButtonItemStylePlain target:self action:@selector(nightAction:)];
//        UIBarButtonItem *night = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"moonset.png"] style:UIBarButtonItemStylePlain target:self action:@selector(nightAction:)];
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
        [button addTarget:self action:@selector(nightAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"月亮.png"] forState:UIControlStateNormal];
        UIBarButtonItem * night = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        [self.navigationItem setRightBarButtonItem:night];
        [DKNightVersionManager setUseDefaultNightColor:YES];
        
        [button release];
        [night release];
    } else {
       // UIBarButtonItem * light = [[UIBarButtonItem alloc] initWithTitle:@"light" style:UIBarButtonItemStylePlain target:self action:@selector(lightAction:)];
        UIBarButtonItem *light = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"太阳.png"] style:UIBarButtonItemStylePlain target:self action:@selector(lightAction:)];
        [self.navigationItem setRightBarButtonItem:light];
        [DKNightVersionManager setUseDefaultNightColor:YES];
        [light release];
    }
    
    
    //  滚动按钮的整个view
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 18)];
    _topView.backgroundColor = [UIColor whiteColor];
    _topView.nightBackgroundColor = UIColorFromRGB(0x343434);
    [self.view addSubview:_topView];
    [_topView release];
    
    // 创建collectionview
    [self createCollectView];
    
    //  button点击的选择view
    _buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, _collect.frame.origin.y - _collect.frame.size.height, WIDTH, _collect.frame.size.height)];
    [_buttonView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.97]];
    
    // 调用创建buttonView的方法
    [self createButtonView];
    [_buttonView setHidden:YES];
    [self.view addSubview:_buttonView];
    [_buttonView release];
    
    [self createScroll];
    
}

#pragma mark -nightAction
- (void)nightAction:(id)sender
{
    [self nightFalls];
    self.light = 0;
    [DKNightVersionManager setUseDefaultNightColor:YES];
    [self createView];
}

- (void)lightAction:(id)sender
{
    [self dawnComes];
    self.light = 1;
    [DKNightVersionManager setUseDefaultNightColor:YES];
    [self createView];
}

- (void)nightFalls {
    [DKNightVersionManager nightFalling];
}

- (void)dawnComes {
    [DKNightVersionManager dawnComing];
}

#pragma mark -创建collectionView
- (void)createCollectView
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(WIDTH, HEIGHT / 18 * 17 - 64 - 49);
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT / 18, WIDTH, HEIGHT / 18 * 17 - 64 - 49) collectionViewLayout:layout];
    _collect.pagingEnabled = YES; // 整页翻动
    _collect.bounces = NO;  // 到头不滑动
    _collect.delegate = self;
    _collect.dataSource = self;
    [_collect registerClass:[NewsCollectCell class] forCellWithReuseIdentifier:@"collect"];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.view addSubview:_collect];
    [_collect setBackgroundColor:[UIColor whiteColor]];
    
    [_collect release];
    [layout release];
}


#pragma mark -collect协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _buttonArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewsCollectCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collect" forIndexPath:indexPath];
    NSDictionary * dic = _buttonArray[indexPath.row];
    cell.delegate = self;
    
    NSMutableArray * arr = [NSMutableArray array];
    // 数据请求
    [SANetWorkingTask request:[dic objectForKey:@"url"] parmater:nil block:^(NSDictionary *result) {
        NSArray * array = [result objectForKey:[dic objectForKey:@"name"]];
        for (NSDictionary * dic1 in array) {
            NewsModel * model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [arr addObject:model];
            [model release];
        }
        [cell createTable:dic array:arr];
    }];
    
    return cell;
}


// 创建buttonView
- (void)createButtonView
{
    // 切换栏目view
    UIView * upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, _buttonView.frame.size.height / 2)];
    UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    label1.nightTextColor = [UIColor lightGrayColor];
    label1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    label1.text = @"  取消栏目";
    label1.nightBackgroundColor = UIColorFromRGB(0x343434);
    [upView addSubview:label1];
    [label1 release];
    
    for (int i = 0; i < self.buttonArray.count; i++) {
        NSDictionary * dic = self.buttonArray[i];
        NSString * title = [dic objectForKey:@"title"];
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH / 4) * (i % 4) + 10, (((upView.frame.size.height - 50) / 5) * (i / 4)) + 50, WIDTH / 5, upView.frame.size.height / 8)];
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1]];
        button.nightTitleColor = [UIColor lightGrayColor];
        [DKNightVersionManager setUseDefaultNightColor:YES];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 15;
        [button.layer setBorderWidth:0.5];
        button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [button setBackgroundColor:[UIColor clearColor]];
        [button addTarget:self action:@selector(buttonCancel:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i + 1;
        [upView addSubview:button];
        [button release];
    }
    
    upView.nightBackgroundColor = UIColorFromRGB(0x343434);
    [_buttonView addSubview:upView];
    [upView release];
    

    // 添加栏目view
    UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, _buttonView.frame.size.height / 2, WIDTH, _buttonView.frame.size.height / 2)];
    UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    label2.text = @"  点击添加更多栏目";
    label2.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    label2.nightBackgroundColor = UIColorFromRGB(0x343434);
    label2.nightTextColor = [UIColor lightGrayColor];
    [downView addSubview:label2];
    [label2 release];
    [_buttonView addSubview:downView];
    
    for (int i = 0; i < _buttonArr.count; i++) {
        NSDictionary * dic = _buttonArr[i];
        NSString * title = [dic objectForKey:@"title"];
        
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake((WIDTH / 4) * (i % 4) + 10, ((downView.frame.size.height - 50) / 5) * (i / 4) + 50, WIDTH / 5, downView.frame.size.height / 8)];
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.nightTitleColor = [UIColor whiteColor];
        [DKNightVersionManager setUseDefaultNightColor:YES];
        [button setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1]];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 15;
        [button.layer setBorderWidth:0.5];
        button.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [button setBackgroundColor:[UIColor clearColor]];
        button.tag = i + _buttonArray.count + 1;
        [button addTarget:self action:@selector(buttonAdd:) forControlEvents:UIControlEventTouchUpInside];
        [downView addSubview:button];
        [button release];
    }
    downView.nightBackgroundColor = UIColorFromRGB(0x343434);
    [_buttonView addSubview:downView];
    [downView release];
}

#pragma mark -buttonView里面button的点击事件

- (void)buttonCancel:(UIButton *)button
{
    NSInteger aaa = button.tag - 1;
    NSDictionary * dic = [self.buttonArray objectAtIndex:aaa];
    [_buttonArr addObject:dic];
    [_buttonArray removeObject:dic];
    [_moreButton setTitle:@"+" forState:UIControlStateNormal];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"buttonArray"];
    [defaults setObject:_buttonArray forKey:@"buttonArray"];
    
    [defaults removeObjectForKey:@"buttonArr"];
    [defaults setObject:_buttonArr forKey:@"buttonArr"];
    [defaults synchronize];
    
    [UIView animateWithDuration:0.6 animations:^
     {
         [_buttonView setFrame:CGRectMake(0, 0 - _collect.frame.size.height, _collect.frame.size.width, _collect.frame.size.height)];
     } completion:^(BOOL finished)
     {
         [_buttonView setHidden:YES];
         [self createView];
     }];
}


- (void)buttonAdd:(UIButton *)button
{
    NSInteger aaa = button.tag - _buttonArray.count - 1;
    NSDictionary * dic = [self.buttonArr objectAtIndex:aaa];
    [self.buttonArray addObject:dic];
    [self.buttonArr removeObject:dic];
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"buttonArray"];
    [defaults setObject:self.buttonArray forKey:@"buttonArray"];
    
    [defaults removeObjectForKey:@"buttonArr"];
    [defaults setObject:self.buttonArr forKey:@"buttonArr"];
    [defaults synchronize];
    
    [UIView animateWithDuration:0.6 animations:^
     {
         [_buttonView setFrame:CGRectMake(0, 0 - _collect.frame.size.height, _collect.frame.size.width, _collect.frame.size.height)];
     } completion:^(BOOL finished)
     {
         [_buttonView setHidden:YES];
         [self createView];
         
         UIButton * button1 = [self.buttonsArray lastObject];
         [self buttonAct:button1];
     }];
}


// hours按钮触发
- (void)allDayAction:(id)sender
{
    TodayNewsViewController * today = [[TodayNewsViewController alloc] init];
    today.navigationController.navigationBar.tintColor = [UIColor redColor];
     today.navigationItem.title = @"24小时要闻";
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,nil];
    [today.navigationController.navigationBar setTitleTextAttributes:attributes];
    [self.navigationController pushViewController:today animated:YES];
    [today release];
}


#pragma mark -创建buttonScroll上面的全部button
- (void)createScroll
{
    _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_moreButton setFrame:CGRectMake(_topView.frame.size.width - 40, 0, 40, _topView.frame.size.height)];
    [_moreButton setBackgroundColor:[UIColor whiteColor]];
    [_moreButton setImage:[UIImage imageNamed:@"向下.png"] forState:UIControlStateNormal];
//    [_moreButton setTitle:@"+" forState:UIControlStateNormal];
    _moreButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [_moreButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _moreButton.nightBackgroundColor = UIColorFromRGB(0x343434);
    _moreButton.nightTitleColor = [UIColor lightGrayColor];
    [_topView addSubview:_moreButton];
    [_moreButton addTarget:self action:@selector(showSelectView:) forControlEvents:UIControlEventTouchUpInside];
    
    _navScroll = [[TouchScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, _topView.frame.size.height)];
    [_navScroll setShowsHorizontalScrollIndicator:NO];
    
    for (int i = 0; i < _buttonArray.count; i++) {
        NSDictionary * dic = _buttonArray[i];
        NSString * title = [dic objectForKey:@"title"];
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(60 * i, 0, 60, _topView.frame.size.height)];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.nightTitleColor = [UIColor lightGrayColor];
        button.tag = i + 1;
        
        if (i == 0) {
            button.titleLabel.textColor = [UIColor redColor];
            button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        } else {
            button.titleLabel.textColor = [UIColor blackColor];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
        [button addTarget:self action:@selector(buttonAct:) forControlEvents:UIControlEventTouchUpInside];
        [_navScroll addSubview:button];
        [self.buttonsArray addObject:button];
    }
    _navScroll.contentSize = CGSizeMake(60 * _buttonArray.count, _topView.frame.size.height);
    [_topView addSubview:_navScroll];
    
}


// 消息中心传值方法
- (void)receive:(NSNotification *)noti
{
    NSDictionary * dic = noti.userInfo;
    
    ShowImageViewController * show = [[ShowImageViewController alloc] init];
    show.skipID = [dic objectForKey:@"url"];
    show.number = 5;
    show.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:show animated:YES];
    [show release];
}


// 展示添加删除列表
- (void)showSelectView:(UIButton *)button
{
    if ([_buttonView isHidden] == YES)
    {
        [_buttonView setHidden:NO];
        [UIView animateWithDuration:0.6 animations:^
         {
             [_buttonView setFrame:CGRectMake(0, _topView.frame.origin.y + _topView.frame.size.height, _collect.frame.size.width, _collect.frame.size.height)];
         } completion:^(BOOL finished)
         {
             [_moreButton setImage:[UIImage imageNamed:@"向上.png"] forState:UIControlStateNormal];
         }];
    }else
    {
        [UIView animateWithDuration:0.6 animations:^
         {
             [_buttonView setFrame:CGRectMake(0, 0 - _collect.frame.size.height, _collect.frame.size.width, _collect.frame.size.height)];
         } completion:^(BOOL finished)
         {
             [_moreButton setImage:[UIImage imageNamed:@"向下.png"] forState:UIControlStateNormal];
             [_buttonView setHidden:YES];
         }];
    }
}



// 点击状态栏按钮跳转
- (void)buttonAct:(UIButton *)button
{
    [_collect setContentOffset:CGPointMake(_collect.frame.size.width * (button.tag - 1), 0)];
    
    for (int i = 0; i < _buttonArray.count ; i++) {
        if (i == button.tag - 1) {
            button.titleLabel.font = [UIFont systemFontOfSize:19];
            button.titleLabel.textColor = [UIColor redColor];
            
        } else {
            UIButton * btn = [self.buttonsArray objectAtIndex:i];
            btn.titleLabel.textColor = [UIColor blackColor];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
        }
    }
    
    
    if (button.tag > 4) {
        [UIView animateWithDuration:0.6 animations:^{
            [self.navScroll setContentOffset:CGPointMake(60 * (button.tag - 5), 0)];
        }];
    } else if (button.tag <= 4) {
        [UIView animateWithDuration:0.6 animations:^{
            [self.navScroll setContentOffset:CGPointMake(0, 0)];
        }];
    }
}


- (void)changeView:(float)x
{
    float xx = x * (60 / self.view.frame.size.width);
    
    float startX = xx;
    int sT = (x)/_collect.frame.size.width + 1;
    
    if (sT <= 0)
    {
        return;
    }
//    UIButton *btn = (UIButton *)[_navScroll viewWithTag:sT];
    UIButton * btn = [self.buttonsArray objectAtIndex:sT - 1];
    float percent = (startX - 60 * (sT - 1)) / 60;
    float value = [QHCcomment lerp:(1 - percent) min:14 max:19];
    btn.titleLabel.font = [UIFont systemFontOfSize:value];
    btn.titleLabel.textColor = [UIColor redColor];
    
    if((int)xx % 60 == 0)
        return;
//    UIButton *btn2 = (UIButton *)[_navScroll viewWithTag:sT + 1];
    UIButton * btn2 = [self.buttonsArray objectAtIndex:sT];
    float value2 = [QHCcomment lerp:percent min:14 max:19];
    btn2.titleLabel.font = [UIFont systemFontOfSize:value2];
    btn2.titleLabel.textColor = [UIColor blackColor];
}


- (void)viewDidLoad {
    [super viewDidLoad];
}


#pragma mark -TableView

- (void)selectCell:(NSInteger)index model:(NewsModel *)model
{
    ShowImageViewController * show = [[ShowImageViewController alloc] init];
    if ([model.skipType isEqualToString:@"photoset"]) {
        show.skipID = model.photosetID;
        show.replayCount = model.replyCount;
        show.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:show animated:YES];
        [show release];
    } else if ([model.skipType isEqualToString:@"special"]) {
        TodaySubjectViewController *subject = [[TodaySubjectViewController alloc]init];
        subject.skipID = model.skipID;
        [self.navigationController pushViewController:subject animated:YES];
        [subject release];
    } else {
        NewsDetailViewController *detail = [[NewsDetailViewController alloc]init];
        detail.docID = model.docid;
        [self.navigationController pushViewController:detail animated:YES];
        [detail release];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _startPointX = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self changeView:scrollView.contentOffset.x];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float xx = scrollView.contentOffset.x * (60 / self.view.frame.size.width) - 60;
    [_navScroll scrollRectToVisible:CGRectMake(xx, 0, _navScroll.frame.size.width, _navScroll.frame.size.height) animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
